package com.ssyt.examinationsystem.controller;

import com.alibaba.fastjson.JSON;
import com.ssyt.examinationsystem.entity.ArrangeInfo;
import com.ssyt.examinationsystem.entity.CourseInfo;
import com.ssyt.examinationsystem.entity.PaperInfo;
import com.ssyt.examinationsystem.entity.TeacherInfo;
import com.ssyt.examinationsystem.service.IArrangeInfoService;
import com.ssyt.examinationsystem.service.impl.ArrangeInfoServiceImpl;
import com.ssyt.examinationsystem.util.DateUtil;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import java.util.Map;

/**
 * Servlet implementation class ArrangeInfoController
 */
public class ArrangeInfoController extends HttpServlet {
    private static final long serialVersionUID = 1L;

    /**
     * @see HttpServlet#HttpServlet()
     */
    public ArrangeInfoController() {
        super();
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
     * response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        doPost(request, response);
    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
     * response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        // 调用安排考试的业务方法
        IArrangeInfoService arrangeService = new ArrangeInfoServiceImpl();
        PrintWriter out = response.getWriter();
        // 获取地址栏上的flag值
        String flag = request.getParameter("flag");
        if ("query".equals(flag)) {
            query(request, response);
        } else if ("initAdd".equals(flag)) {
            initAdd(request, response);
        } else if ("list".equals(flag)) {
            list(arrangeService, request, response, out);
        } else if ("add".equals(flag)) {
            add(arrangeService, request, response, out);
        } else if ("dele".equals(flag)) {
            delete(arrangeService, request, response, out);
        } else if ("update".equals(flag)) {
            update(arrangeService, request, response, out);
        }

        out.flush();
        out.close();
    }

    private void update(IArrangeInfoService arrangeService, HttpServletRequest request, HttpServletResponse response,
                        PrintWriter out) {
        // 获取表单数据
        String arrangeId = request.getParameter("arrangeId");
        String paperId = request.getParameter("paperId");
        String classId = request.getParameter("classId");
        String teaId = request.getParameter("teaId");
        String leader = request.getParameter("leader");
        String startTime = request.getParameter("startTime");
        String endTime = request.getParameter("endTime");
        String address = request.getParameter("address");
        String courseId = request.getParameter("courseId");
        // 创建考试安排对象，并封装属性
        ArrangeInfo arrange = new ArrangeInfo();

        PaperInfo paper = new PaperInfo();
        paper.setPaperId(Integer.valueOf(paperId));
        arrange.setPaper(paper);

        arrange.setClassId(classId);
        ;
        TeacherInfo tea = new TeacherInfo();
        tea.setTeaId(Integer.valueOf(teaId));
        arrange.setTea(tea);

        TeacherInfo lead = new TeacherInfo();
        lead.setTeaId(Integer.valueOf(leader));
        arrange.setLeader(lead);

        arrange.setStartTime(DateUtil.convertStringToDate(startTime));
        arrange.setEndTime(DateUtil.convertStringToDate(endTime));

        arrange.setAddress(address);
        CourseInfo course = new CourseInfo();
        course.setCourseId(Integer.valueOf(courseId));
        arrange.setCourse(course);
        arrange.setArrangeId(Integer.valueOf(arrangeId));
        // 调用修改业务方法
        if (arrangeService.updateArrange(arrange) > 0) {
            out.print("ok");
        }
    }

    /**
     * 删除考试安排
     *
     * @param arrangeService
     * @param request
     * @param response
     * @param out
     */
    private void delete(IArrangeInfoService arrangeService, HttpServletRequest request, HttpServletResponse response,
                        PrintWriter out) {
        String arrangeId = request.getParameter("arrangeId");

        if (arrangeService.deleteArrange(arrangeId) > 0) {
            out.print("ok");
        }
    }

    /**
     * 添加考试安排信息
     *
     * @param arrangeService
     * @param request
     * @param response
     * @param out
     */
    private void add(IArrangeInfoService arrangeService, HttpServletRequest request, HttpServletResponse response,
                     PrintWriter out) {
        // 获取表单数据
        String paperId = request.getParameter("paperId");
        String classId = request.getParameter("classId");
        String teaId = request.getParameter("teaId");
        String leader = request.getParameter("leader");
        String startTime = request.getParameter("startTime");
        String endTime = request.getParameter("endTime");
        String address = request.getParameter("address");
        String courseId = request.getParameter("courseId");
        // 创建考试安排对象，并封装属性
        ArrangeInfo arrange = new ArrangeInfo();

        PaperInfo paper = new PaperInfo();
        paper.setPaperId(Integer.valueOf(paperId));
        arrange.setPaper(paper);

        arrange.setClassId(classId);
        ;
        TeacherInfo tea = new TeacherInfo();
        tea.setTeaId(Integer.valueOf(teaId));
        arrange.setTea(tea);

        TeacherInfo lead = new TeacherInfo();
        lead.setTeaId(Integer.valueOf(leader));
        arrange.setLeader(lead);

        arrange.setStartTime(DateUtil.convertStringToDate(startTime));
        arrange.setEndTime(DateUtil.convertStringToDate(endTime));

        arrange.setAddress(address);
        CourseInfo course = new CourseInfo();
        course.setCourseId(Integer.valueOf(courseId));
        arrange.setCourse(course);
        // 调用添加业务方法
        if (arrangeService.addArrange(arrange) > 0) {
            out.print("ok");
        }

    }

    /**
     * 显示安排信息
     *
     * @param arrangeService
     * @param request
     * @param response
     * @param out
     */
    private void list(IArrangeInfoService arrangeService, HttpServletRequest request, HttpServletResponse response,
                      PrintWriter out) {
        List<Map<String, Object>> arrangeList = arrangeService.getArranges();
        String arrangeJson = JSON.toJSONString(arrangeList, true);
        out.print(arrangeJson);
    }

    /**
     * 转发到安排考试页面
     *
     * @param request
     * @param response
     * @throws IOException
     * @throws ServletException
     */
    private void initAdd(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        request.getRequestDispatcher("WEB-INF/views/arrange/arrangeAdd.jsp").forward(request, response);

    }

    /**
     * 转发到考试安排信息维护页面
     *
     * @param request
     * @param response
     * @throws IOException
     * @throws ServletException
     */
    private void query(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.getRequestDispatcher("WEB-INF/views/arrange/arrangeList.jsp").forward(request, response);

    }

}
